﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;
using System.Data;
using Comm;
using BusinessFacade;
using Entities;

namespace MedicareNoteManagerWeb.Web
{
    public partial class PersonGroupManageAdd : BasePage
    {
        Busi_med_persongroupmanage model1 = new Busi_med_persongroupmanage();
        public static string ID = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                model1 = (Busi_med_persongroupmanage)Session["PersonGroupManageModel"];
                BindDrop();
                try
                {
                     ID = Request.QueryString["ID"].ToString();
                }
                catch (Exception ex)
                {
                    lbl_Message.ToolTip = ex.Message;
                }
                if (ID !="")
                {
                    FillValue();
                }
            }
        }


        public void FillValue()
        {
            Button4.Text = "修改";
            Busi_med_persongroupmanage u = new Busi_med_persongroupmanage();
            med_persongroupmanage_Model model = u.GetListModel(Convert.ToInt32(ID));
            DropArea.SelectedIndex = model.Area;
            cbopersonstate.Text = model.PersonState;
            txt_personage.Text = model.PersonAge.ToString();
            cbopersonsex.Text = model.PersonSex;
            txt_paymentterm.Text = model.PaymentTerm.ToString();
            txt_arrearageterm.Text = model.ArrearageTerm.ToString();
            txt_paymentbase.Text = model.PaymentBase.ToString();
            cbosubordinateunits.Text = model.SubordinateUnits;
        }

        public void BindDrop()
        {
            FillDropDownList("GetDrop", "AreaID", "AreaName", "请选择区域", this.DropArea);
        }

        /// <summary>
        /// 判断字符
        /// </summary>
        /// <returns></returns>
        public string CheckData()
        {
            string strErr = "";
            if (DropArea.SelectedValue == "0")
            {
                strErr += "请选择区域";
                return strErr;
            }

            if (cbopersonstate.SelectedValue == "0")
            {
                strErr += "请选择状态";
                return strErr;
            }

            if (string.IsNullOrEmpty(txt_personage.Text.Trim()))
            {
                strErr += "年龄不能为空";
                return strErr;
            }

            if (cbopersonsex.SelectedValue == "0")
            {
                strErr += "请选择性别";
                return strErr;
            }

            if (txt_paymentterm.Text == "")
            {
                strErr += "缴费年限不能为空";
                return strErr;
            }

            if (txt_arrearageterm.Text == "")
            {
                strErr += "欠费年限不能为空";
                return strErr;
            }

            if (txt_paymentbase.Text == "")
            {
                strErr += "缴费基数不能为空";
                return strErr;
            }
            if (cbosubordinateunits.SelectedValue == "0")
            {
                strErr += "请选择单位";
                return strErr;
            }
            return strErr;
        }

        /// <summary>
        /// 清空值
        /// </summary>
        protected void ClearValue()
        {
            txt_personage.Text = "";
            cbopersonsex.SelectedIndex = 0;
            txt_paymentterm.Text = "";
            txt_arrearageterm.Text = "";
            txt_paymentbase.Text = "";
            DropArea.SelectedIndex = 0;
            cbosubordinateunits.SelectedIndex = 0;
            cbopersonstate.SelectedIndex = 0;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button4_Click(object sender, EventArgs e)
        {
            model1 = (Busi_med_persongroupmanage)Session["NoteplanModel"];
            string strErr = CheckData();
            try
            {
                if (string.IsNullOrEmpty(strErr))
                {
                    if (ID=="")
                    {
                        med_persongroupmanage_Model model = new med_persongroupmanage_Model();
                        model.PersonMoney = Convert.ToDecimal(1000.00);
                        model.PersonAge = Convert.ToInt32(txt_personage.Text);
                        model.PersonSex = cbopersonsex.SelectedValue.ToString();
                        model.PaymentTerm = Convert.ToInt32(txt_paymentterm.Text);
                        model.ArrearageTerm = Convert.ToInt32(txt_arrearageterm.Text);
                        model.PaymentBase = Convert.ToDecimal(txt_paymentbase.Text);
                        model.Area = Convert.ToInt32(DropArea.SelectedValue);
                        model.SubordinateUnits = cbosubordinateunits.SelectedValue.ToString();
                        model.PersonState = cbopersonstate.SelectedValue.ToString();
                        if (new Busi_med_persongroupmanage().Add(model))
                        {
                            MessageBox.ShowDialog(this.Page, "添加成功！", "success");
                            lbl_Message.Text = "添加成功！";
                            ClearValue();
                            return;
                        }
                        else
                        {
                            MessageBox.ShowDialog(this.Page, "添加失败！", "error");
                        }
                    }
                   else
                    {
                        med_persongroupmanage_Model model2 = new med_persongroupmanage_Model();
                        model2.Area = int.Parse(DropArea.SelectedValue);
                        model2.ArrearageTerm = Convert.ToInt32(txt_arrearageterm.Text);
                        model2.PaymentBase = Convert.ToDecimal(txt_paymentbase.Text);
                        model2.PaymentTerm = Convert.ToInt32(txt_paymentterm.Text);
                        model2.PersonAge = Convert.ToInt32(txt_personage.Text);
                        model2.PersonMoney = Convert.ToDecimal(0);
                        model2.PersonSex = cbopersonsex.SelectedValue;
                        model2.PersonState = cbopersonstate.SelectedValue.ToString();
                        model2.SubordinateUnits = cbosubordinateunits.SelectedValue;
                        new Busi_med_persongroupmanage().Update(model2);
                        MessageBox.ShowDialog(this.Page, "修改成功！", "success");
                        lbl_Message.Text = "修改成功";

                    }
                }
            }
            catch (System.Exception ex)
            {
                lbl_Message.Text = ex.Message;
            }
        }



        protected void Cancel_Click(object sender, EventArgs e)
        {
           
        }
    }
}